﻿<?php
function tf_create_functions_file() {
  global $thfr_css;

  $string = tf_create_functions_file_widgets();

  // Fallback for menus
  $string .= tf_create_functions_file_pagemenu();
  $string .= tf_create_functions_file_catmenu();

  $string .= tf_create_functions_file_add_js();

  $string .= tf_create_functions_file_custom_comments();
  $string .= tf_create_functions_file_excert();

  $string .= tf_create_functions_file_add_common();

  //$string .= tf_create_functions_file_my_breadcrumbs();

  $string .= $thfr_css['customphp'] . '?>';

  return $string;
}

function tf_create_functions_file_widgets() {
  global $thfr_css, $aAvailableDropAreas;

  $string = '<?php
// Helper function to avoid repetitive code when creating widget areas
function register_these_widget_areas($names) {
  foreach($names as $name) {
    register_sidebar(array(
      "name" => $name,
      "before_widget" => \'<div id="%1$s" class="widget %2$s">\',
      "after_widget" => "</div>",
      "before_title" => "<h3>",
      "after_title" => "</h3>"
    ));
  }
}

// 6 Default Sidebar Widget Areas
register_these_widget_areas(array("LEFT-1", "LEFT-2", "LEFT-3", "RIGHT-1", "RIGHT-2", "RIGHT-3"));
';

  // Create Custom Widget Areas
  $widgetAreaArray = array();
  foreach (array(
    'index',
    'home',
    'page',
    'single'
  ) as $pageTemplates) {
    foreach ($aAvailableDropAreas as $dropArea) {
      for ($i = 0; $i <= 24; $i++) {
        switch ($thfr_css[$pageTemplates][$dropArea . '-' . $i]) {
          case "widgetarea_1":
            $widgetAreaArray[] = "WidgetArea-1";
            break;
          case "widgetarea_1_2":
            $widgetAreaArray[] = "WidgetArea-1-2";
            break;
          case "widgetarea_1_3":
            $widgetAreaArray[] = "WidgetArea-1-3";
            break;
          case "widgetarea_2":
            array_push($widgetAreaArray, 'WidgetArea-2 Col-1', 'WidgetArea-2 Col-2');
            break;
          case "widgetarea_2_2":
            array_push($widgetAreaArray, 'WidgetArea-2-2 Col-1', 'WidgetArea-2-2 Col-2');
            break;
          case "widgetarea_3":
            array_push($widgetAreaArray, 'WidgetArea-3 Col-1', 'WidgetArea-3 Col-2', 'WidgetArea-3 Col-3');
            break;
          case "widgetarea_4":
            array_push($widgetAreaArray, 'WidgetArea-4 Col-1', 'WidgetArea-4 Col-2', 'WidgetArea-4 Col-3', 'WidgetArea-4 Col-4');
            break;
          case "widgetarea_5":
            array_push($widgetAreaArray, 'WidgetArea-5 Col-1', 'WidgetArea-5 Col-2', 'WidgetArea-5 Col-3', 'WidgetArea-5 Col-4', 'WidgetArea-5 Col-5');
            break;
          case "widgetarea_6":
            array_push($widgetAreaArray, 'WidgetArea-6 Col-1', 'WidgetArea-6 Col-2', 'WidgetArea-6 Col-3', 'WidgetArea-6 Col-4', 'WidgetArea-6 Col-5', 'WidgetArea-6 Col-6');
            break;
        }
      }
    }
  }
  if (isset($widgetAreaArray)) {
    // remove double entries:
    $widgetAreaArray = array_unique($widgetAreaArray);
    // print code for custom widget areas
    $string .= "register_these_widget_areas(array('" . implode("','", $widgetAreaArray) . "'));";
  }

  return $string;
}

function tf_create_functions_file_pagemenu() {
  return '
function tf_pagemenu() {
  echo \'<div id="hormenu" class="clearfix"><ul id="nav" class="menu">\';
  wp_list_pages("title_li=");
  echo \'</ul></div>\';
}
';
}

function tf_create_functions_file_catmenu() {
  return '
function tf_catmenu() {
  echo \'<div id="hormenu2" class="clearfix"><ul id="nav2" class="menu">\';
  wp_list_categories("title_li=");
  echo \'</ul></div>\';
}
';
}

function tf_create_functions_file_add_common() {
  return '
// Add Spam and Delete links to comments
function delete_comment_link($id) {
  if (current_user_can("edit_post")) {
    echo \' | <span class="comment-delete-link"><a href="\'.admin_url("comment.php?action=cdc&c=$id").\'">Delete</a></span>\';
    echo \' | <span class="comment-spam-link"><a href="\'.admin_url("comment.php?action=cdc&dt=spam&c=$id").\'">Spam</a></span>\';
  }
}

if ( function_exists( "add_theme_support" ) ) {
  add_theme_support( "post-thumbnails" );
  set_post_thumbnail_size( 200, 200, true );
  add_image_size( "single-post-thumbnail", 400, 9999 ); // Permalink thumbnail size
  
  add_theme_support("automatic-feed-links");
}
  
if (function_exists("register_nav_menus")) {
add_action( "init", "register_new_menus" );
  function register_new_menus() {
    register_nav_menus(
      array(
        "menu1" => __( "Menu 1" ),
        "menu2" => __( "Menu 2" )
      )
    );
  }
}

// Not used but required to have by the wordpress.org theme check
if ( ! isset( $content_width ) )
  $content_width = 640;
';
}

function tf_create_functions_file_add_js() {
  return '
if ( !is_admin() ) {
  wp_enqueue_script("jquery");
  wp_register_script("my_javascript", get_template_directory_uri() . "/javascript.js", array("jquery"), "1.0" );
  wp_enqueue_script("my_javascript");
}
';
}

function tf_create_functions_file_custom_comments() {
  global $thfr_css;

  return '
// Custom comments
function thfr_comments($comment, $args, $depth) {
$GLOBALS["comment"] = $comment; ?>
<li <?php comment_class($class="clearfix") ?> id="comment-<?php comment_ID(); ?>">
  <div id="div-comment-<?php comment_ID(); ?>" class="clearfix comment-container<?php
  $comment = get_comment($comment_id);
  if ( $post = get_post($post_id) ) {
    if ( $comment->user_id === $post->post_author )
      echo " bypostauthor";
  } ?>">
    <div class="comment-links-block">
    <?php if ($args["avatar_size"] != 0) echo get_avatar( $comment, $args["avatar_size"] ); ?>
    <span class="comment-author-name"><?php comment_author_link(); ?></span> |
    <span class="comment-date-link">
      <a href="<?php echo htmlspecialchars( get_comment_link( $comment->comment_ID ) ) ?>">
      <?php echo get_comment_date() . " в " .  get_comment_time(); ?></a>
    </span>
    <?php echo comment_reply_link(array(
      "before" => " | <span class=\"comment-reply-link\">",
      "after" => "</span>",
      "reply_text" => "' . $thfr_css['commenttext']['reply_text'] . '",
      "depth" => $depth,
      "max_depth" => $args["max_depth"]
      ));
    ?>
    <?php edit_comment_link(__("Edit")," | <span class=\"comment-edit-link\">","</span>") ?>
    <?php delete_comment_link(get_comment_ID()); ?><br />
	</div>
	
    <?php if ($comment->comment_approved == "0") : ?>
      <em><?php _e("Your comment is awaiting moderation.") ?></em><br />
    <?php endif; ?>
	
    <?php comment_text() ?>
  </div>
<?php
}
';
}

function tf_create_functions_file_excert() {
  global $thfr_css;

  return '
// Excerpt Length:
function tf_excerpt_length($length) {
  return ' . $thfr_css['excerpt']['length'] . ';
}
add_filter("excerpt_length", "tf_excerpt_length");

// Excerpt Read More:
function tf_excerpt_more($more) {
  global $post;
  return str_replace( array( "%title%", "%url%" ), array( the_title("","",FALSE), get_permalink($post->ID) ), "' . str_replace('"', '\\"', $thfr_css['excerpt']['readmore']) . '" );
}
add_filter("excerpt_more", "tf_excerpt_more");
';
}

function tf_create_functions_file_my_breadcrumbs() {
  return '
// Breadcrumbs
function my_breadcrumbs() {
  global $post, $wp_query, $author;
  
  $home = "Home";
  $separator = "&rarr;";
  $before = "<span class=current>";
  $after = "</span>";

  if ( is_home() OR is_front_page() )  { $separator = ""; }

  echo \'<a href="\' . home_url() . \'">\' . $home . \'</a> \' . $separator . \' \';

  if ( is_category() ) {
    $cat_obj = $wp_query->get_queried_object();
    $thisCat = $cat_obj->term_id;
    $thisCat = get_category($thisCat);
    $parentCat = get_category($thisCat->parent);
    if ($thisCat->parent != 0)
      echo get_category_parents($parentCat, TRUE,  " $separator ");
    echo $before . single_cat_title("", false) . $after;
    
  } elseif ( is_day() ) {
    echo \'<a href="\' . get_year_link(get_the_time("Y")) . \'">\' . get_the_time("Y") . \'</a> \' . $separator . \' \';
    echo \'<a href="\' . get_month_link(get_the_time("Y"),get_the_time("m")) . \'">\' . get_the_time("F") . \'</a> \' . $separator . \' \';
    echo $before . get_the_time("d") . $after;
    
  } elseif ( is_month() ) {
    echo \'<a href="\' . get_year_link(get_the_time("Y")) . \'">\' . get_the_time("Y") . \'</a> \' . $separator . \' \';
    echo $before . get_the_time("F") . $after;
    
  } elseif ( is_year() ) {
    echo $before . get_the_time("Y") . $after;
    
  } elseif ( is_single() AND !is_attachment() ) {
    if ( get_post_type() != \'post\' ) {
      $post_type = get_post_type_object(get_post_type());
      $slug = $post_type->rewrite;
      echo \'<a href="\' . home_url() . \'/\' . $slug[\'slug\'] . \'/">\' . $post_type->labels->singular_name . \'</a> \' . $separator . \' \';
      echo $before . the_title(\'\',\'\',false) . $after;
    } else {
      $cat = get_the_category(); $cat = $cat[0];
      echo get_category_parents($cat, TRUE, " $separator ");
      echo $before . the_title(\'\',\'\',false) . $after;
    }
    
  } elseif ( !is_single() AND !is_page() AND get_post_type() != \'post\' ) {
    $post_type = get_post_type_object(get_post_type());
    echo $before . $post_type->labels->singular_name . $after;
    
  } elseif ( is_attachment() ) {
    $parent = get_post($post->post_parent);
    $cat = get_the_category($parent->ID); $cat = $cat[0];
    echo get_category_parents($cat, TRUE, " $separator ");
    echo \'<a href="\' . get_permalink($parent) . \'">\' . $parent->post_title . \'</a> \' . $separator . \' \';
    echo $before . the_title(\'\',\'\',false) . $after;
    
  } elseif ( is_page() AND !$post->post_parent ) {
    echo $before . the_title(\'\',\'\',false) . $after;
    
  } elseif ( is_page() AND $post->post_parent ) {
    $parent_id  = $post->post_parent;
    $breadcrumbs = array();
    while ($parent_id) {
      $page = get_page($parent_id);
      $breadcrumbs[] = \'<a href="\' . get_permalink($page->ID) . \'">\' . get_the_title($page->ID) . \'</a>\';
      $parent_id  = $page->post_parent;
    }
    $breadcrumbs = array_reverse($breadcrumbs);
    foreach ($breadcrumbs as $singleCrumb) {
      echo "$singleCrumb $separator ";
    }
    echo $before . the_title(\'\',\'\',false) . $after;
    
  } elseif ( is_search() ) {
    echo $before . \'Search results for "\' . get_search_query() . \'"\' . $after;
    
  } elseif ( is_tag() ) {
    echo $before . \'Posts tagged "\' . single_tag_title("", false) . \'"\' . $after;
    
  } elseif ( is_author() ) {
    $userdata = get_userdata($author);
    echo $before . \'Articles posted by \' . $userdata->display_name . $after;
    
  } elseif ( is_404() ) {
    echo $before . \'Error 404\' . $after;
  }

  if ( get_query_var(\'paged\') ) {
    if ( is_category() OR is_day() OR is_month() OR is_year() OR is_search() OR is_tag() OR is_author() )
      echo \' (\';
    echo \'Page \' . get_query_var(\'paged\');
    if ( is_category() OR is_day() OR is_month() OR is_year() OR is_search() OR is_tag() OR is_author() )
      echo \')\';
  }
}
';
}

?>